﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Data.GameData;
using Data.GameData.GameMap;
using Data.GameData.Utils;
using Data.GameData.Interfaces;

namespace GameFramework.Orders
{
    public class MovementOrder : Order
    {
        IFleetElement unit = null;
        MapIndex destination = null;
        List<MapIndex> waypoints = new List<MapIndex>();

        public MovementOrder()
        {
            Type = OrderType.MOVEMENT;
        }

        public IFleetElement Unit
        {
            get { return unit; }
            set { unit = value; }
        }

        public MapIndex Destination
        {
            get
            {
                if (waypoints.Count == 0)
                    return unit.MapPosition;
                else
                    return waypoints.Last();
                //return destination;
            }
            set
            {
                waypoints.Insert(waypoints.Count, value);
                destination = value;
            }
        }

        #region Waypoint Management
        public IEnumerable<MapIndex> Waypoints
        {
            get { return waypoints; }
        }

        public void AddWaypoint(MapIndex waypoint)
        {
            waypoints.Add(waypoint);
        }

        public void AddWaypoints(IEnumerable<MapIndex> waypoints)
        {
            this.waypoints.AddRange(waypoints);
        }

        public void RemoveWaypoint(int index)
        {
            waypoints.RemoveAt(index);
        }

        public void RemoveAll()
        {
            waypoints.Clear();
        }
        #endregion

        public override void Fulfill()
        {
            if (unit.MapPosition == destination)
                Status = OrderStatusType.COMPLETE;
            else
                Status = OrderStatusType.IN_PROGRESS;
        }
    }
}
